/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseManager;

import java.sql.*;
import javax.naming.*;
import javax.servlet.http.HttpServlet;
import javax.sql.DataSource;

/**
 *
 * @author 47
 */
public class DbConn extends HttpServlet{

    public DbConn() {
        super();
    }

    public static java.sql.Connection getNewDbconn() {
        Connection conn = null;
        Context ctx;
        DataSource ds;
        try {
            ctx = new InitialContext();
            ds = (DataSource) ctx.lookup(resourceString);
            conn = ds.getConnection();
        } catch (Exception ex) {
            msg=ex.getMessage();
            ex.printStackTrace();
            conn=null;
        }
        return conn;
    }
    //jdbc驱动
    String dbdriver;
    //连接字符串
    String connstr;
    //用户名
    String username;
    //密码
    String password;

    public void initDefault(){
        this.dbdriver=this.getInitParameter("dbaddr");
    }
    //消息
    protected static String msg = "";

    /**
     * Get the value of msg
     *
     * @return the value of msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * Set the value of msg
     *
     * @param msg new value of msg
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    //数据源
    protected static String resourceString = "jdbc/fb";

    /**
     * Get the value of resourceString
     *
     * @return the value of resourceString
     */
    public String getResourceString() {
        return resourceString;
    }

    /**
     * Set the value of resourceString
     * resourceString指的是在web.xml中配置配置的数据源
     * 默认使用 jdbc/fb
     *
     * <resource-ref>
     *<res-ref-name>jdbc/fb</res-ref-name>
     *<res-type>javax.sql.DataSource</res-type>
     *<res-auth>Container</res-auth>
     *<res-sharing-scope>Shareable</res-sharing-scope>
     *</resource-ref>
     *
     * @param resourceString new value of resourceString
     */
    public void setResourceString(String resourceString) {
        this.resourceString = resourceString;
    }
}
